package com.xue.web.service;

import com.alibaba.fastjson.JSONObject;
import com.xue.web.dao.RegisterDao;

import java.util.List;
import java.util.Map;

public class RegisterService {
    public JSONObject doRegister(String userName, String userPassword, String userEmail) {
        System.out.println(this.getClass().getSimpleName() + "|执行注册业务");
        JSONObject jsonObject = new JSONObject();
        RegisterDao registerDao = new RegisterDao();

        Map<String, List<Object>> stringListMap = registerDao.selectUser(userName);
        List<Object> username = stringListMap.get("user_id");
        if (username==null|| username.size()==0){
            System.out.println(this.getClass().getSimpleName()+"|没有找到数据,开始写入数据库");
            boolean insertUser = registerDao.insertUser(userName, userPassword, userEmail);
            if (insertUser){
                jsonObject.put("code",200);
                jsonObject.put("msg","注册成功");
                return jsonObject;
            }else {
                System.out.println(this.getClass().getSimpleName()+"|警告:插入用户时异常");
                jsonObject.put("code",400);
                jsonObject.put("msg","账户名已经存在");
                return jsonObject;
            }
        }else {
            jsonObject.put("code",400);
            jsonObject.put("msg","账户名已经存在");
            return jsonObject;
        }

    }
}
